using System;
using System.Security.Cryptography;
using System.Text;

namespace Util.Extensions
{
    public static class Encryption
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <returns></returns>
        public static string ToMD5String(this string str)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            buffer = MD5.Create().ComputeHash(buffer);
            string result = BitConverter.ToString(buffer).Replace("-", "").ToLower();
            return result;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <param name="key">加入的密钥</param>
        /// <returns></returns>
        public static string ToMD5String(this string str, string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new Exception("密钥不能为空");
            }
            string buf = $"{key.ToLower()}{str}{key.ToUpper()}";
            string result = buf.ToMD5String();
            return result;
        }

        /// <summary>
        /// DES加密(和别的系统对接)
        /// </summary>
        /// <param name="str">要進行DES加密的字串</param>
        /// <param name="key">密鋝</param>
        /// <param name="isUrl">是否放到URL中，如果是就要對加密后的字串進行特殊字符編碼</param>
        /// <returns></returns>
        public static string DesEncrypt(this string str, string key)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.UTF8.GetBytes(str);
            des.Mode = CipherMode.ECB;
            des.Key = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            //des.IV = des.Key;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            string ret = Convert.ToBase64String(ms.ToArray());

            return ret;
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="str">要進行DES解密的字串</param>
        /// <param name="key">密鋝</param>
        /// <param name="isUrl">是否放到URL中，如果是就要對密文進行反向編碼再解密</param>
        /// <returns></returns>
        public static string DesDecrypt(this string str, string key)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = CipherMode.ECB;
            des.Key = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            //des.IV = des.Key;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            byte[] inputByteArray = Convert.FromBase64String(str);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            string ret = Encoding.UTF8.GetString(ms.ToArray());
            return ret;
        }
    }
}
